package com.bardsoftware.foronuvolo.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;

public class MessageSender {
    protected final TextArea messageField;
    protected final Button sendButton;
	
    public MessageSender(
            RootPanel textAreaPanel,
            RootPanel sendButtonPanel,
            final MessageSendService sendService,
            final String outputElementID,
            final String errorElementID) {
        sendButton = new Button("Send");
        sendButton.setStylePrimaryName("gp-button");

        messageField = new TextArea();
        messageField.setSize("40em", "100%");

        // Add the components to a panel

        textAreaPanel.add(messageField);
        sendButtonPanel.add(sendButton);
        
        addSendHandler(sendService);

    }

	protected void addSendHandler(final MessageSendService sendService) {
		class SendHandler implements ClickHandler, KeyUpHandler {
            public void onClick(ClickEvent event) {
                sendTextToServer();
            }
            public void onKeyUp(KeyUpEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER && event.isControlKeyDown()) {
                    sendTextToServer();
                }
            }
            private void sendTextToServer() {
                sendButton.setEnabled(false);
                //createNewTagButton.setEnabled(false);
                String textToServer = messageField.getText();
                sendService.sendMessage(
                        textToServer,
                        new AsyncCallback<String>() {
                            public void onFailure(Throwable caught) {
                                MessageSender.this.onFailure(caught);
                            }
                            public void onSuccess(String result) {
                                MessageSender.this.onSuccess(result);
                            }
                        });
            }
        }

        // Add a handler to send the name to the server
        SendHandler handler = new SendHandler();
        
        //createNewTagButton.addClickHandler(handler);
        sendButton.addClickHandler(handler);
        messageField.addKeyUpHandler(handler);
	}

    public TextArea getMessageField() {
        return messageField;
    }
    protected Button getSendButton() {
        return sendButton;
    }
    protected void onSuccess(String result) {
    }
    protected void onFailure(Throwable caught) {
    }
    protected void showErrorNotifier(String id) {
        final Element notifier = Document.get().getElementById(id);
        notifier.setAttribute("style", "display:block");
        Timer t = new Timer() {
            @Override
            public void run() {
                notifier.setAttribute("style", "display:none");
                getSendButton().setEnabled(true);
            }
        };
        t.schedule(5000);
    }

}
